package Environment;


import FrameWork.Audio.RSAudio;
import FrameWork.IPlay;
import FrameWork.MainCanvas;
import GamePlay.GamePlay;
import GamePlay.Global;
import java.io.IOException;
import javax.microedition.lcdui.game.GameCanvas;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ThanhTri
 */
public class Game extends GameCanvas implements Runnable {
    boolean m_iRunning;
    long m_Time;
    float m_DetaTime,m_SaveTime;    
    int m_CountFPS; 
    public IPlay m_Play;
    Thread m_Thread;
    private float fps;
    public Game () throws IOException {
        super (false);
        fps =0;
        m_SaveTime=0;
        m_CountFPS =0;
        m_Time=System.currentTimeMillis();
        setFullScreenMode(true);  
        m_Play = new GamePlay(getGraphics()); 
        m_Play.Init();
        MainCanvas.Instance().mGame = this;
    }
    // Start game
    public void Start (){
        m_iRunning = true;
        m_Thread = new Thread (this);
        m_Thread.start();

    }
    // Game Loop
    public void run() {
        while (true){
            m_DetaTime =(float)(System.currentTimeMillis()-m_Time)/1000.0f;
            m_Time = System.currentTimeMillis();
            if(m_iRunning == true){
                try {
                    ProcessKey();
                    
                    m_CountFPS++;
                    m_SaveTime += m_DetaTime;
                    if(m_SaveTime>= 1.0f){
                        fps = m_CountFPS/m_SaveTime;
                        System.out.println("FPS :"+ fps );
                       
                        m_SaveTime -= 1.0f;
                        m_CountFPS=0;
                    }
                    
                    Update(m_DetaTime);
                    Render();
//                     RSFont.getRS().getFont(Global.FONT_BIG).DrawString(
//                                "FPS:"+(int)fps, 180, 0, getGraphics());
                      flushGraphics();
                    if (m_DetaTime<0.016f){
                        Thread.sleep(16-(int)(m_DetaTime*1000));
                    }
                    
                    
                } catch (InterruptedException ex){
                    ex.printStackTrace();
                }
            }
        }
    }
    void ProcessKey (){
        int _Keycode = getKeyStates ();
       m_Play.getCurState().MyKey(_Keycode);
    }
      protected void keyPressed(int keyCode){
        System.out.println("Keycode: "+ keyCode );
        m_Play.getCurState().KeyPressed(keyCode);
    }
    protected void keyRepeated(int keyCode){
        m_Play.getCurState().KeyRepeated(keyCode);
    }
    protected void keyReleased(int keyCode){
        m_Play.getCurState().KeyReleased(keyCode);
    }
    protected void pointerPressed(int x, int y){
        m_Play.getCurState().PointerPressed(x, y);
    }
    protected void hideNotify(){
        m_iRunning = false;
    }
    protected void showNotify(){
       m_iRunning = true ;
       if(m_Play.getCurState().getID() == Global.STATE_HELP ||
               m_Play.getCurState().getID() == Global.STATE_LEVEL ||
               m_Play.getCurState().getID() == Global.STATE_LOST ||
               m_Play.getCurState().getID() == Global.STATE_MAINGAME ||
               m_Play.getCurState().getID() == Global.STATE_MENU ||
               m_Play.getCurState().getID() == Global.STATE_MENUINGAME ||
               m_Play.getCurState().getID() == Global.STATE_WIN ||
               m_Play.getCurState().getID() == Global.STATE_OPTION)
           if(RSAudio.getRS().getEnable()){
               RSAudio.getRS().PlayBack();
           }
    }
    
    void Update (float _Time){
       m_Play.Update(_Time);
    }
    void Render (){        
       m_Play.Render();
      
    }
}

